package com.yeyks.common.utils;

import com.google.common.collect.Lists;

import java.util.List;

/**
 * @author fuchangshuai
 * @date 2019/08/08 14:33
 */
public class DateListUtil {

    /**
     * 获取月份的所有天数(格式2019-01)，如2019-01，获取1月的所有天数
     *
     * @param yyyyMM
     * @return
     */
    public static List<String> getMonthAllDays(String yyyyMM) {
        if (null == yyyyMM) {
            return null;
        }
        List<String> dayLists = Lists.newArrayList("01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30");
        List<Integer> thirtyOneList = Lists.newArrayList(1, 3, 5, 7, 8, 10, 12);
        List<Integer> thirtyList = Lists.newArrayList(4, 6, 9, 11);
        try {
            String month = yyyyMM.substring(yyyyMM.length() - 2);
            if ("0".equals(month.substring(0, 1))) {
                month = month.replaceAll("0", "");
            }
            String year = yyyyMM.substring(0, 4);
            Integer intMonth = Integer.valueOf(month);
            Integer intYear = Integer.valueOf(year);
            if (thirtyOneList.contains(intMonth)) {
                dayLists.add("31");
                return getMonthAllDays(yyyyMM, dayLists);
            }
            if (thirtyList.contains(intMonth)) {
                return getMonthAllDays(yyyyMM, dayLists);
            }
            if (2 == intMonth) {
                if (((intYear % 4 == 0) && (intYear % 100 != 0)) || (intYear % 400 == 0)) {
                    dayLists.remove("30");
                } else {
                    dayLists.removeAll(Lists.newArrayList("30", "29"));
                }
                return getMonthAllDays(yyyyMM, dayLists);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    private static List<String> getMonthAllDays(String year, List<String> dayLists) {
        List<String> results = Lists.newArrayList();
        dayLists.forEach(e -> {
            results.add(year + "-" + e);
        });
        return results;
    }

    public static void main(String[] args) {
        List<String> monthAllDays = getMonthAllDays("2019-01");
        System.out.println(monthAllDays);
    }

}
